﻿using System.Reflection;
using Autofac;

namespace Messhall.Framework.Autofac;

public abstract class ComponentScansanBase
{

    protected IContainer _container;

    public ComponentScansanBase()
    {
        
    }
    
    
    
    public static ContainerBuilder AddScansanServices( ContainerBuilder builder, string repositoryNamespaceBase, string repositoryNamespace, string domainNamespace, string applicationNamespace)
    {
        var repositoryBase = Assembly.Load(repositoryNamespaceBase);
        var repository = Assembly.Load(repositoryNamespace);
        var domain = Assembly.Load(domainNamespace);
        var application = Assembly.Load(applicationNamespace);

        builder.RegisterAssemblyTypes(repositoryBase, repositoryBase).InstancePerLifetimeScope();

        builder.RegisterAssemblyTypes(repositoryBase, repository).InstancePerLifetimeScope();//.Where(type => type.Name.EndsWith(""));
        builder.RegisterAssemblyTypes(domain, repository).Where(type => type.Name.EndsWith("Repository")).InstancePerLifetimeScope();
        builder.RegisterAssemblyTypes(domain).Where(type => type.Name.EndsWith("Service")).InstancePerLifetimeScope();
        builder.RegisterAssemblyTypes(application);

        builder.RegisterBuildCallback(scope =>
        {
            AutofacBuilder.SetContainer((IContainer)scope);
        });
        return builder;
    }
}